package com.ky.study.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.ky.common.config.SystemConfig;
import com.ky.common.param.TousuAdd;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.study.entity.Static;
import com.ky.study.entity.Tousu;
import com.ky.study.mapper.StaticMapper;
import com.ky.study.mapper.TousuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 投诉 前端控制器
 * </p>
 *
 * @author wgz
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/study/tousu")
public class TousuController {
    @Autowired
    TousuMapper tousuMapper;
    @Autowired
    StaticMapper staticMapper;
    @RequestMapping("/addTousu")
    public R addTousu(HttpServletRequest request, @RequestBody TousuAdd tousuAdd){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        Tousu tousu = new Tousu();
        tousu.setUserId(id);
        tousu.setArticleId(tousuAdd.getArticleId());
        tousu.setComplaintType(tousuAdd.getComplaintType());
        tousu.setDescription(tousuAdd.getDescription());
        tousu.setCreatedAt(new Date());
        tousuMapper.insert(tousu);
        for (String image : tousuAdd.getUrlList()) {
            Static staticResource = new Static();
            staticResource.setTousuId(tousu.getTousuId());
            staticResource.setFileUrl(image);
            staticResource.setCreatedAt(new Date());
            staticMapper.insert(staticResource);
        }
        return R.ok();
    }

}

